/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.util.memcached.ZimbraMemcachedClient;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.ldap.LdapProvisioning;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PrintStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ProxyPurgeUtil
/*     */ {
/*     */   static final String memcachedPort = "11211";
/*     */   
/*     */   public static void main(String[] args)
/*     */     throws ServiceException
/*     */   {
/*  55 */     boolean purge = false;
/*     */     
/*     */ 
/*  58 */     String logLevel = "ERROR";
/*     */     CommandLine commandLine;
/*     */     try
/*     */     {
/*  62 */       commandLine = parseCommandLine(args);
/*     */     } catch (ParseException pe) {
/*  64 */       commandLine = null;
/*     */     }
/*     */     
/*  67 */     if ((commandLine == null) || (commandLine.hasOption("h")) || (commandLine.hasOption("u")))
/*     */     {
/*     */ 
/*     */ 
/*  71 */       usage();
/*  72 */       System.exit(1);
/*     */     }
/*     */     
/*  75 */     if (commandLine.hasOption("v")) logLevel = "DEBUG";
/*  76 */     ZimbraLog.toolSetupLog4j(logLevel, null, false);
/*     */     
/*     */ 
/*  79 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  84 */     List<Server> memcachedServers = prov.getAllServers("memcached");
/*  85 */     ArrayList<String> servers = new ArrayList();
/*     */     
/*  87 */     for (Iterator<Server> it = memcachedServers.iterator(); it.hasNext();) {
/*  88 */       Server s = (Server)it.next();
/*  89 */       String serverName = s.getAttr("zimbraServiceHostname", "localhost");
/*  90 */       String servicePort = s.getAttr("zimbraMemcachedBindPort", "11211");
/*  91 */       servers.add(serverName + ":" + servicePort);
/*     */     }
/*     */     
/*  94 */     ArrayList<String> accounts = getAccounts(commandLine);
/*  95 */     servers.addAll(getCacheServers(commandLine));
/*     */     
/*  97 */     if (servers.size() == 0) {
/*  98 */       System.err.println("No memcached servers found, and none specified (--help for help)");
/*  99 */       System.exit(1);
/*     */     }
/*     */     
/* 102 */     if (accounts.size() == 0) {
/* 103 */       System.err.println("No accounts specified (--help for help)");
/* 104 */       System.exit(1);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 111 */     purge = !commandLine.hasOption("i");
/*     */     String outputformat;
/*     */     String outputformat;
/* 114 */     if (commandLine.hasOption("o")) {
/* 115 */       outputformat = commandLine.getOptionValue("o");
/*     */     } else {
/* 117 */       outputformat = "[%1$s] %2$s -- %3$s";
/*     */     }
/*     */     
/* 120 */     purgeAccounts(servers, accounts, purge, outputformat);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void purgeAccounts(List<String> servers, List<String> accounts, boolean purge, String outputformat)
/*     */     throws ServiceException
/*     */   {
/* 134 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*     */ 
/* 137 */     if ((accounts == null) || (accounts.isEmpty())) {
/* 138 */       System.err.println("No account supplied");
/* 139 */       System.exit(1);
/*     */     }
/*     */     
/* 142 */     if (!purge)
/*     */     {
/* 144 */       if ((outputformat == null) || (outputformat.length() == 0)) {
/* 145 */         System.err.println("outputformat must be supplied for info");
/* 146 */         System.exit(1);
/*     */       }
/*     */     }
/*     */     Iterator<Server> it;
/* 150 */     if (servers == null) {
/* 151 */       List<Server> memcachedServers = prov.getAllServers("memcached");
/* 152 */       servers = new ArrayList();
/*     */       
/* 154 */       for (it = memcachedServers.iterator(); it.hasNext();) {
/* 155 */         Server s = (Server)it.next();
/* 156 */         String serverName = s.getAttr("zimbraServiceHostname", "localhost");
/* 157 */         String servicePort = s.getAttr("zimbraMemcachedBindPort", "11211");
/* 158 */         servers.add(serverName + ":" + servicePort);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 164 */     int numServers = servers.size();
/* 165 */     ArrayList<ZimbraMemcachedClient> zmcs = new ArrayList();
/*     */     
/* 167 */     for (int i = 0; i < numServers; i++) {
/* 168 */       ZimbraMemcachedClient zmc = new ZimbraMemcachedClient();
/* 169 */       zmc.connect(new String[] { (String)servers.get(i) }, false, null, 0, 5000L);
/* 170 */       zmcs.add(zmc);
/*     */     }
/*     */     
/* 173 */     for (String a : accounts)
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 202 */       ArrayList<String> routes = new ArrayList();
/*     */       
/*     */ 
/* 205 */       Account account = prov.get(Key.AccountBy.name, a);
/* 206 */       if (account == null)
/*     */       {
/* 208 */         System.out.println("error looking up accout: " + a);
/* 209 */         routes.add("route:proto=http;user=" + a);
/* 210 */         routes.add("route:proto=imap;user=" + a);
/* 211 */         routes.add("route:proto=pop3;user=" + a);
/* 212 */         routes.add("route:proto=httpssl;user=" + a);
/* 213 */         routes.add("route:proto=imapssl;user=" + a);
/* 214 */         routes.add("route:proto=pop3ssl;user=" + a);
/*     */       } else {
/* 216 */         String uid = account.getUid();
/* 217 */         routes.add("route:proto=http;id=" + account.getId());
/* 218 */         routes.add("route:proto=http;user=" + uid);
/* 219 */         routes.add("route:proto=imap;user=" + uid);
/* 220 */         routes.add("route:proto=pop3;user=" + uid);
/* 221 */         routes.add("route:proto=httpssl;id=" + account.getId());
/* 222 */         routes.add("route:proto=httpssl;user=" + uid);
/* 223 */         routes.add("route:proto=imapssl;user=" + uid);
/* 224 */         routes.add("route:proto=pop3ssl;user=" + uid);
/* 225 */         routes.add("route:proto=httpssl;admin=1;id=" + account.getId());
/*     */         
/* 227 */         String domain = account.getDomainName();
/* 228 */         routes.add("route:proto=http;user=" + uid + "@" + domain);
/* 229 */         routes.add("route:proto=imap;user=" + uid + "@" + domain);
/* 230 */         routes.add("route:proto=pop3;user=" + uid + "@" + domain);
/* 231 */         routes.add("route:proto=httpssl;user=" + uid + "@" + domain);
/* 232 */         routes.add("route:proto=imapssl;user=" + uid + "@" + domain);
/* 233 */         routes.add("route:proto=pop3ssl;user=" + uid + "@" + domain);
/* 234 */         routes.add("alias:user=" + uid + ";ip=" + domain);
/*     */         
/* 236 */         Domain d = prov.get(Key.DomainBy.name, domain);
/* 237 */         String[] vips = d.getVirtualIPAddress();
/* 238 */         for (String vip : vips)
/*     */         {
/* 240 */           routes.add("route:proto=http;user=" + uid + "@" + vip);
/* 241 */           routes.add("route:proto=imap;user=" + uid + "@" + vip);
/* 242 */           routes.add("route:proto=pop3;user=" + uid + "@" + vip);
/* 243 */           routes.add("route:proto=httpssl;user=" + uid + "@" + vip);
/* 244 */           routes.add("route:proto=imapssl;user=" + uid + "@" + vip);
/* 245 */           routes.add("route:proto=pop3ssl;user=" + uid + "@" + vip);
/* 246 */           routes.add("alias:user=" + uid + ";ip=" + vip);
/*     */         }
/* 248 */         String[] vhostnames = d.getVirtualHostname();
/* 249 */         for (String vhost : vhostnames)
/*     */         {
/* 251 */           routes.add("alias:user=" + uid + ";vhost=" + vhost);
/*     */         }
/*     */         
/* 254 */         String[] aliases = account.getMailAlias();
/* 255 */         List<String> uids = new ArrayList();
/* 256 */         uids.add(uid);
/* 257 */         for (String alias : aliases) {
/* 258 */           if ((alias.indexOf('@') != -1) && (alias.substring(alias.indexOf('@') + 1).equals(domain))) {
/* 259 */             uids.add(alias.substring(0, alias.indexOf('@')));
/*     */           }
/*     */         }
/*     */         
/*     */ 
/* 264 */         if ((prov instanceof LdapProvisioning)) {
/* 265 */           ZLdapContext ldpCtx = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.GET_DOMAIN);
/*     */           try {
/* 267 */             List<String> aliasDomainIds = ((LdapProvisioning)prov).getEmptyAliasDomainIds(ldpCtx, d, false);
/* 268 */             if (aliasDomainIds != null)
/* 269 */               for (String aliasDomainId : aliasDomainIds) {
/* 270 */                 aliasDomain = prov.getDomainById(aliasDomainId).getDomainName();
/* 271 */                 for (String userName : uids) {
/* 272 */                   routes.add("route:proto=http;user=" + userName + "@" + aliasDomain);
/* 273 */                   routes.add("route:proto=imap;user=" + userName + "@" + aliasDomain);
/* 274 */                   routes.add("route:proto=pop3;user=" + userName + "@" + aliasDomain);
/* 275 */                   routes.add("route:proto=httpssl;user=" + userName + "@" + aliasDomain);
/* 276 */                   routes.add("route:proto=imapssl;user=" + userName + "@" + aliasDomain);
/* 277 */                   routes.add("route:proto=pop3ssl;user=" + userName + "@" + aliasDomain);
/* 278 */                   routes.add("alias:user=" + userName + ";ip=" + aliasDomain);
/*     */                 }
/*     */               }
/*     */           } finally {
/*     */             String aliasDomain;
/* 283 */             LdapClient.closeContext(ldpCtx);
/*     */           }
/*     */         }
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 290 */         for (String alias : aliases) {
/* 291 */           routes.add("route:proto=http;user=" + alias);
/* 292 */           routes.add("route:proto=imap;user=" + alias);
/* 293 */           routes.add("route:proto=pop3;user=" + alias);
/* 294 */           routes.add("route:proto=httpssl;user=" + alias);
/* 295 */           routes.add("route:proto=imapssl;user=" + alias);
/* 296 */           routes.add("route:proto=pop3ssl;user=" + alias);
/*     */           
/* 298 */           if (alias.indexOf('@') != -1) {
/* 299 */             alias = alias.substring(0, alias.indexOf('@'));
/*     */           }
/* 301 */           for (String vhost : vhostnames)
/*     */           {
/* 303 */             routes.add("alias:user=" + alias + ";vhost=" + vhost);
/*     */           }
/* 305 */           for (String vip : vips)
/*     */           {
/* 307 */             routes.add("route:proto=http;user=" + alias + "@" + vip);
/* 308 */             routes.add("route:proto=imap;user=" + alias + "@" + vip);
/* 309 */             routes.add("route:proto=pop3;user=" + alias + "@" + vip);
/* 310 */             routes.add("route:proto=httpssl;user=" + alias + "@" + vip);
/* 311 */             routes.add("route:proto=imapssl;user=" + alias + "@" + vip);
/* 312 */             routes.add("route:proto=pop3ssl;user=" + alias + "@" + vip);
/* 313 */             routes.add("alias:user=" + alias + ";ip=" + vip);
/*     */           }
/*     */         }
/*     */       }
/*     */       ZimbraMemcachedClient zmc;
/* 318 */       for (int i = 0; i < numServers; i++) {
/* 319 */         zmc = (ZimbraMemcachedClient)zmcs.get(i);
/*     */         
/* 321 */         for (String route : routes) {
/* 322 */           if (purge)
/*     */           {
/*     */ 
/* 325 */             System.out.println("Purging " + route + " on server " + (String)servers.get(i));
/* 326 */             zmc.remove(route, false);
/*     */           } else {
/* 328 */             String output = String.format(outputformat, new Object[] { servers.get(i), route, zmc.get(route) });
/* 329 */             System.out.println(output);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 335 */     for (ZimbraMemcachedClient zmc : zmcs) {
/* 336 */       zmc.disconnect(-1L);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static ArrayList<String> getAccounts(CommandLine commandLine)
/*     */   {
/* 346 */     ArrayList<String> accounts = new ArrayList();
/* 347 */     String[] values = commandLine.getOptionValues("a");
/* 348 */     String filename = commandLine.getOptionValue("L");
/*     */     
/*     */ 
/* 351 */     if (values != null) {
/* 352 */       for (String u : commandLine.getOptionValues("a")) {
/* 353 */         accounts.add(u);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 360 */     if (filename != null) { BufferedReader br;
/*     */       BufferedReader br;
/* 362 */       if (filename == "-") {
/* 363 */         br = new BufferedReader(new InputStreamReader(System.in));
/*     */       } else {
/*     */         try
/*     */         {
/* 367 */           br = new BufferedReader(new FileReader(filename));
/*     */         } catch (FileNotFoundException e) {
/* 369 */           br = null;
/* 370 */           System.err.println("File not found: " + filename);
/*     */         }
/*     */       }
/*     */       
/* 374 */       if (br != null) {
/*     */         String s;
/*     */         do {
/* 377 */           try { s = br.readLine();
/*     */           } catch (IOException e) {
/* 379 */             s = null;
/*     */           }
/*     */           
/* 382 */           if (s != null) {
/* 383 */             accounts.add(s);
/*     */           }
/* 385 */         } while (s != null);
/*     */         try {
/* 387 */           br.close();
/*     */         }
/*     */         catch (IOException e) {}
/*     */       }
/*     */     }
/* 392 */     return accounts;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static ArrayList<String> getCacheServers(CommandLine commandLine)
/*     */   {
/* 401 */     ArrayList<String> servers = new ArrayList();
/* 402 */     String[] values = commandLine.getArgs();
/*     */     
/* 404 */     if (values != null) {
/* 405 */       for (String s : commandLine.getArgs()) {
/* 406 */         servers.add(s);
/*     */       }
/*     */     }
/*     */     
/* 410 */     return servers;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static CommandLine parseCommandLine(String[] args)
/*     */     throws ParseException
/*     */   {
/* 424 */     CommandLineParser parser = new GnuParser();
/* 425 */     Options options = new Options();
/*     */     
/* 427 */     options.addOption("h", "help", false, "print usage");
/* 428 */     options.addOption("u", "usage", false, "print usage");
/* 429 */     options.addOption("v", "verbose", false, "be verbose");
/* 430 */     options.addOption("i", "info", false, "display route information");
/* 431 */     options.addOption("a", "account", true, "account name");
/* 432 */     options.addOption("L", "list", true, "file containing list of accounts, one per line");
/* 433 */     options.addOption("o", "output", true, "format for displaying routing information");
/*     */     
/* 435 */     return parser.parse(options, args);
/*     */   }
/*     */   
/*     */ 
/*     */   static void usage()
/*     */   {
/* 441 */     System.err.println(" ");
/* 442 */     System.err.println(" Purges POP/IMAP routing information from one or more memcached servers");
/* 443 */     System.err.println(" Available Memcached servers are discovered by the 'zmprov gamcs' function, others can be specified");
/* 444 */     System.err.println(" If necessary, please specify additional memcached servers in the form of server:port at the end of the command line");
/* 445 */     System.err.println(" ");
/* 446 */     System.err.println(" Usage: zmproxypurge [-v] [-i] -a account [-L accountlist] [cache1 [cache2 ... ]]");
/* 447 */     System.err.println("  -h, --help     Display help");
/* 448 */     System.err.println("  -v, --verbose  Be verbose");
/* 449 */     System.err.println("  -i, --info     Just display account routing information, don't purge (dry run)");
/* 450 */     System.err.println("  -a, --account  Account name");
/* 451 */     System.err.println("  -L, --list     File containing list of accounts, one per line");
/* 452 */     System.err.println("                 Use `-' to indicate standard input");
/* 453 */     System.err.println("  -o, --output   Format to be used to print routing information with -i");
/* 454 */     System.err.println("                 Three fields are displayed by default:");
/* 455 */     System.err.println("                 . the cache server");
/* 456 */     System.err.println("                 . the account name");
/* 457 */     System.err.println("                 . the route information");
/* 458 */     System.err.println("                 Default format is `[%1$s] %2$s -- %3$s'");
/* 459 */     System.err.println("  cacheN         (Optional) Additional memcache server, of the form server:port");
/* 460 */     System.err.println(" ");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/ProxyPurgeUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */